SMngModule = angular.module("SMngModule", [ 'SellerModule', 'ui.router','MapModule']);

SMngModule.config(function($stateProvider, $urlRouterProvider) {

	$urlRouterProvider.otherwise("/stores");
	$stateProvider.state('stores', {
		url : "/stores",
		templateUrl : 'management/stores.html',
		controller : 'SStoresCtrl'
	});

	$stateProvider.state('lists', {
		url : "/lists",
		templateUrl : 'management/lists.html',
		controller : 'SListsCtrl'
	});

	$stateProvider.state('products', {
		url : '/products',
		templateUrl : 'management/products.html',
		controller : 'SProductsCtrl'
	});
	$stateProvider.state('account-mng', {
		url : '/account-mng',
		templateUrl : 'management/account-mng.html',
		controller : 'SProductsCtrl'
	});

});

SMngModule.factory('Store', function($resource) {
	return $resource("/api/Store/:id", {
		sellerId : sessionStorage.getItem("idSeller"),
		id : '@id'
	});
});

SMngModule.controller("emptyCtrl", function() {
});

SMngModule.controller("SMngCtrl", function($scope, $state, $http, $window) {
	$scope.isState = function(stateName) {
		return $state.includes(stateName);

	}
	$scope.logOut = function() {

		var out = function() {
			$window.location.href = "/seller/";
		}
		$http.get("/api/seller/logout").then(out, out);
	}
});

SMngModule.controller("SStoresCtrl", function($scope, $window, Seller,Map) {
	Map.initMap("map");
	
	$scope.currentStore = {};
	$scope.stores = [];
	$scope.stores = Seller.myStores();

	$scope.showStore = function(idStore) {
		//Map.initMap("map");
		Map.locateMe();
		console.log("loading stored with id: " + idStore);
		$scope.currentStore = Seller.getStore({
			id : idStore
	},function(data){
		
	})
		
	}
	$scope.newStore = {};
	$scope.showFastNameModal = function() {
		$scope.newStore = {};
		$("#fastNameModal").modal('show');
		
	}
	$scope.createStore = function() {
		var ns = Seller.newStore(undefined, {
			fastName : $scope.newStore.fastName
		}, function() {			
			$("#fastNameModal").modal('hide');
			$scope.stores.push(ns);
		}, function(error) {			
			$scope.newStore.errorMessage = error.data;

		})
	}

});

SMngModule.controller("SListsCtrl", function($scope) {

});

SMngModule.controller("SProductsCtrl", function($scope) {

});